<?php
!defined('M_P') && exit('Forbidden');
$SCR = 'info';
//评论相关
InitGP(array('hid', 'page'), 'GP', 2);
InitGP(array('step'), 'GP', 1);
!$hid && Showmsg("undefined_action", "index.php?m=house");
$baseurl = $houseBaseUrl."q=evaluations&hid=$hid";
!$page && $page = 0;

@require_once M_P . '/lib/evaluations.class.php';
$pw_evaluation_db = House::loadDB('evaluation');
$pw_evaluation = new PW_Evaluations($pw_evaluation_db);

$houseService = house::loadClass('house');/* @var $houseService PW_House */
$houseService->setHid($hid);

if (!$houseService->init(true)) {
	Showmsg('该楼盘不存在!');
}
$houseService->setNav();
$houseService->setBanner();
$houseinfo = & $houseService->info;

if ($step == 'evaledit') {
	define('AJAX',1);
	InitGP(array('content', 'jumpurl'), 'GP', 1);
	$uid = $winduid ? $winduid : '0';
	$_message = "点评成功\treload";
	
	//是否允许游客发表评论
	if ($groupid == 'guest' && !$house_evaluation_open) {
		echo "游客禁止点评，请您先登录"; ajax_footer();exit();
	}
	
	if(empty($content)){
		echo "点评内容不能为空"; ajax_footer();exit();
	}
	
	$wordsfb = L::loadClass('FilterUtil', 'filter');
	if (($banword = $wordsfb->comprise($content, false)) !== false) {
		Showmsg('content_wordsfb');
	}
	$hname = $houseinfo['name'];
	$ctype = $pw_evaluation_db->_ctype['evaluation'];
	$ifcheck = $house_evaluation_check ? '0' : '1';
	if(isGM($windid)){
		$ifcheck = 1;
	}
	$_pwsql = array(
		array($uid, $hid, $hname, $timestamp, $ctype, $content, $ifcheck));
	$pw_evaluation_db->insertHouseEvaluations($_pwsql);
	$pageCache = getPageCache('info', $hid);
	$pageCache->deleteCache('evaluations');
	
	if ($house_evaluation_check && !isGM($windid) ) {
		$_message = "点评发表成功，已开启点评审核功能，等待管理员审核！\treload";
	}
	
	echo $_message; ajax_footer();
	exit();
} else if ($step == 'evaldel') {
	InitGP(array('id'), 'GP', 2);
	!$id && Showmsg("undefined_action");
	$pw_evaluation_db->deleteHouseEvaluationsByID($id, $hid);
	$pageCache = getPageCache('info', $hid);
	$pageCache->deleteCache('evaluations');
	echo "删除成功\treload"; ajax_footer();
	exit();
} else if ($step == 'score') {
	InitGP(array('starinfo'), 'GP');
	empty($starinfo) && Showmsg("非法的评分值");
	$stararray = explode(',', $starinfo);
	$_content_1 = $_content_2 = array();
	if (!empty($stararray)) {
		foreach ($stararray as $value) {
			list($_type, $_star) = explode(':', $value);
			$_score = ceil((int) $_star * (int) $pw_evaluation->getAverageScore());
			$_content_1[$_type] = array($_score);
			$_content_2[$_type] = $_star;
		}
	}
	$pw_evaluation_db->insertScoreInfoByHid($hid, $_content_1);
	
	//记录用户评分信息
	$pw_evaluation->setUserScoreInfo($_content_2, $hid, $winduid);
	unset($_score, $_content_2, $_content_1);
	echo "success";
	ajax_footer();
	exit();
}

$pw_evaluation_db->setDb_perpage(10);
$pw_evaluation_db->setBasename($baseurl);
!$page && $page = 1;
$conditions = array('hid' => array($hid, 'eq'));

$evaluations = $pw_evaluation_db->searchHouseEvaluation($conditions, $page, 'front');

//评分相关
$scoreCount = $pw_evaluation->getScoreTypeCount();
$scoreKeys = $pw_evaluation->getScoreTypeList();
$scoreKeys = implode(',', $scoreKeys);
$starhtml = $pw_evaluation->getStarHtml($hid, $winduid);
list($scorehtml, $scoreinfo, $totalScore, $count) = $pw_evaluation->getScoreHtml($hid);
$fullScore = $pw_evaluation->fullScore;

$pageCache = getPageCache('other', '');
$pageCache->init();

//URL setting
$houseScore = $houseBaseUrl.'q=evaluations&hid=' . $hid;
$houseInfo = $houseBaseUrl.'q=info&hid=' . $hid;
$imageTypeUrl = $houseBaseUrl.'q=album&hid=' . $hid;
$mapurl = $houseBaseUrl."q=housemap&hid=$hid";

/* SEO */
$_definedSeo = array('title' => $houseinfo['seotitle'], 'metaDescription' => $houseinfo['seokeywords'], 
	'metaKeywords' => $houseinfo['seodescription']);
$_seopage = 'read';
$_seoname = $houseinfo['name'];

require_once (M_P . 'require/header.php');
require_once PrintEot('evaluations');
footer();
?>